<?php
	if ($_REQUEST['add']){
		$text = text2html(escapestr($_REQUEST["text"]));
    $subject  = escapestr($_REQUEST['subject']);
		$query = "INSERT INTO news (`lang`,`text`, `subject` ,`time`) VALUES ('$lang','$text','$subject','".time()."')";
		$result = mysql_query($query);
		if ($result) {
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_news"),GetLangString($lang,"msg_newsadded"));
		} else {	
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
		}
	} else  
	if ($_REQUEST['delete']){
		$deleteme=escapestr($_REQUEST['delete']);
		$query = "DELETE from `news` where `id`='$deleteme'";
		$result = mysql_query($query);		
		if ($result) {
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_news"),GetLangString($lang,"msg_newsdeleted"));
		} else {	
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
		}
	} else 
	if ($_REQUEST['editdone']){
		$editme   = escapestr($_REQUEST['id']);
		$newslang = escapestr($_REQUEST['lang']);
		$subject  = escapestr($_REQUEST['subject']);
		$text     = escapestr($_REQUEST['text']);
		$time = time();
		$query = "UPDATE `news` SET
			`lang` 		= '$newslang',
			`subject` 	= '$subject',
			`text`      = '$text',
			`edited`    = '$time'
			WHERE `id` = '$editme'";
		$result = mysql_query($query);
		if ($result) {
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_news"),GetLangString($lang,"msg_newsedited"));
		} else {	
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
		}		
			
	}
	
	if ($_REQUEST['edit']){
		$editme = escapestr($_REQUEST['edit']);
		$query = "select * from `news` where `id`='$editme'";
		$result = mysql_query($query); 
		if ($result) {
		$news = mysql_fetch_array($result);
		
		$EDITNEWS = '<form method="POST" ACTION="index.php?do=admin&admindo=news">';
		$EDITNEWS .= "<input type='hidden' name='id' value='$editme'>";
		$EDITNEWS .= "<select name='lang' size='1'>";
		foreach($supportedlangs as $supportedlang) {
			$EDITNEWS .= "<option value='$supportedlang[0]'";
			if ($supportedlang[0] == $news['lang']) $EDITNEWS .= " selected";
			$EDITNEWS .=">$supportedlang[1]</option>";
		}
		$EDITNEWS .="</select>" ;
		$EDITNEWS .= "<input type='text' name='subject' value='".$news['subject']."'>";
		$EDITNEWS .= '<textarea name="text" rows="15">'.html2text($news['text']).'</textarea>';
		
		$EDITNEWS .= "<input value=\"".GetLangString($lang,"txt_edit")."\"	name = \"editdone\" 	type=\"submit\">" ;
		$EDITNEWS .= '</form>';
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_editnews"),$EDITNEWS);
		
		
		
		} else {	
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
		}		
	} else {
	
	//$query = "Select * from `news` where `lang`='$lang'";
		$query = "Select * from `news`";
		$result 		=	 mysql_query($query);
		if (mysql_num_rows($result)) {
			while ($news = mysql_fetch_array($result)){
			$query_lang = "SELECT `text` from `text` where `type`='txt_lang' and `lang` = '" .$news['lang'] ."'";	
			$result_lang = mysql_query($query_lang);
			$text_lang = mysql_result($result_lang,0);	
			$time = str_ireplace("%datetime%",date("d-m-Y H:i:s",$news['time']),GetLangString($lang,"txt_newsadded"));
			$sitenews = FormatSmallElement( $time . "   -   ".
				$text_lang . "  -  " . 
				"<a href=\"index.php?do=admin&admindo=news&edit=".$news['id']."\">".GetLangString($lang,"txt_edit")."</a>  -  ".
				"<a href=\"index.php?do=admin&admindo=news&delete=".$news['id']."\">".GetLangString($lang,"txt_delete")."</a>"
				, $news['text'] ) . $sitenews;
			}
		}	

		$SITE_MIDDLE .= FormatElement( GetLangString ($lang, "txt_news") , bb2html($sitenews) ) ;
	
		$ADDNEWS = '<form method="POST" ACTION="index.php?do=admin&admindo=news">';
		$ADDNEWS .= "<select name='lang' size='1'>";
		foreach($supportedlangs as $supportedlang) {
			$ADDNEWS .= "<option value='$supportedlang[0]'";
			if ($supportedlang[0] == $lang) $ADDNEWS .= " selected";
			$ADDNEWS .=">$supportedlang[1]</option>";
		}
		$ADDNEWS .="</select>" ;
		$ADDNEWS .= "<input type='text' name='subject'>";
		$ADDNEWS .= '<textarea name="text" rows="5"></textarea>';
		

		$ADDNEWS .= "<input value=\"".GetLangString($lang,"txt_save")."\"	name = \"add\" 	type=\"submit\">" ;
		$ADDNEWS .= '</form>';
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_addnews"),$ADDNEWS);
	}
?>
